<?php
namespace Tea\Controller;
use Common\Controller\AdminbaseController;
use Tea\Model\TeaModel;
use Tea\Model\TeaWorksModel;

/**
 * Created by PhpStorm.
 * User: weichen
 * Date: 2017/5/7
 * Time: 18:25
 */
class TeaController extends AdminbaseController
{
    private $tea_model;
    private $tea_works_model;

    public function __construct()
    {
        parent::__construct();
        $this->tea_model = new TeaModel();
        $this->tea_works_model = new TeaWorksModel();
    }

    public function lists() {
        $where = [];
        $count = $this->tea_model
            ->alias('a')
            ->join('LEFT JOIN cmf_member as b ON a.mid = b.id')
            ->where($where)
            ->count();
        $page = $this->page($count, C("PAGE_NUMBER"));
        $result = $this->tea_model
            ->alias('a')
            ->join('LEFT JOIN cmf_member as b ON a.mid = b.id')
            ->limit($page->firstRow . ',' . $page->listRows)
            ->where($where)
            ->field('a.*, b.username')
            ->select();
        foreach ($result as $k => $v) {
            if($v['status'] ==TeaModel::STATUS_ON) {
                $result[$k]['str_manage'] = '<a data-toggle="modal" data-target="#myModal"  class="add_ext"  onclick=""   name="' . $v['id'] . '">编辑</a> | <a class="js-ajax-btn-dialog" href="' . U('Tea/complete', ['id' => $v['id']]) . '">结束批次任务</a>';
            } elseif ($v['status'] == TeaModel::STATUS_OK) {
                $result[$k]['str_manage'] = '<a data-toggle="modal" data-target="#myModal"  class="add_ext"  onclick=""   name="' . $v['id'] . '">编辑</a> | <a class="js-ajax-btn-dialog js-ajax-delete" href="' . U('Tea/delete', ['id' => $v['id']]) . '">删除</a>';
            }
            $number_sn = '<a class="js-ajax-btn-dialog " href="' . U('Tea/detail', ['id' => $v['id']]) . '">'.$v['number_sn'].'</a>';
            $lists .= '<tr>
                <td>' . ($k + 1) . '</td>
                <td>' . $v['id'] . '</td>
                <td>' . $number_sn . '</td>
                <td>' . $v['username'] . '</td>
                <td>' . $v['create_time'] . '</td>
                <td style="white-space:nowrap;">' . $result[$k]['str_manage']    . '</td>
            </tr>';
        }

        $this->assign('lists', $lists);
        $this->display();
    }

    public function detail() {
        $id = I('get.id');
        $details = $this->tea_model->where(['id' => $id])->find();
        $details['status'] = $this->tea_model->statusToString($details['status']);//dump($details);

        $works = $this->tea_works_model->where(['number_sn' => $details['number_sn']])->select();
        foreach ($works as $k => $v) {
            $lists .= '<tr>
                <td>' . ($k + 1) . '</td>
                <td>' . $v['id'] . '</td>
                <td>' . $this->tea_works_model->typeToString($v['type']) . '</td>
                <td>' . $v['create_time'] . '</td>
            </tr>';
        }

        $this->assign('info', $details);
        $this->assign('works', $lists);
        $this->display();
    }
}